package com.ruoyi.other.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.other.domain.EPersonnel;
import com.ruoyi.other.service.EPersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Null;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

import static com.ruoyi.common.core.utils.PageUtils.startPage;
import static com.ruoyi.common.core.web.controller.BaseController.getDataTable;


@RestController
@RequestMapping("/personnel")
public class EPersonnelController {
    @Autowired
    EPersonnelService ePersonnelService;

    /**
     * 会员人数
     */
    @RequiresPermissions("other:personnel:vip")
    @GetMapping("/vip")
    public Integer getVip(String vip) {
        return ePersonnelService.getVip(vip);
    }


    /**
     * 快递员人数
     */
    @RequiresPermissions("other:personnel:courier")
    @GetMapping("/courier")
    public Integer getCourier() {
        return ePersonnelService.getCourier();
    }

    /**
     * 普通会员人数
     */
    @RequiresPermissions("other:personnel:commonVip")
    @GetMapping("/commonVip")
    public Integer getCommonVip() {
        return ePersonnelService.getCommonVip();
    }

    @RequiresPermissions("other:personnel:courierList")
    @GetMapping("/courierList")
    public TableDataInfo courierList() {
        List<EPersonnel> list = ePersonnelService.selectCourierList();
        return getDataTable(list);
    }

    @RequiresPermissions("other:personnel:pid")
    @GetMapping("/pid")
    public AjaxResult Pid() {
        List<EPersonnel> list = ePersonnelService.selectCourierList();
        // 使用Stream流进行过滤和分组统计
        Map<String, Long> typeCountMap = list.stream()
                .filter(x -> !ObjectUtil.isNull(x.getMemberType()))
                .collect(Collectors.groupingBy(EPersonnel::getMemberType, Collectors.counting()));

        //返回给前端的数据结构
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(typeCountMap)) {
            for (String key : typeCountMap.keySet()) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", key);
                map.put("value", typeCountMap.get(key));
                mapList.add(map);
            }
        }
        return AjaxResult.success(mapList);
    }

    /**
     * 昨日新增会员人数
     */
    @RequiresPermissions("other:personnel:yesterdayVip")
    @GetMapping("/yesterdayVip")
    public Integer getYesterdayVip(String yesterdayVip) {
        return ePersonnelService.getYesterdayVip(yesterdayVip);
    }

    /**
     * i
     * 昨日新增快递员人数
     */
    @RequiresPermissions("other:personnel:yesterdayCcourier")
    @GetMapping("yesterdayCcourier")
    public Integer getYesterdayCcourier() {
        return ePersonnelService.getYesterdayCcourier();
    }

    /**
     * 昨日新增普通会员人数
     */
    @RequiresPermissions("other:personnel:yesterdayCommonVip")
    @GetMapping("/yesterdayCommonVip")
    public Integer getYesterdayCommonVip() {
        return ePersonnelService.getYesterdayCommonVip();
    }

    @RequiresPermissions("other:personnel:chartContainer")
    @GetMapping("/chartContainer")
    public AjaxResult chartContainer() {
        List<EPersonnel> list = ePersonnelService.selectCourierList();

        // 获取前一天的时间范围
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startOfDayYesterday = now.minusDays(1).truncatedTo(ChronoUnit.DAYS);
        LocalDateTime endOfDayYesterday = startOfDayYesterday.plusDays(1).minusNanos(1);

        long startTimestamp = startOfDayYesterday.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        long endTimestamp = endOfDayYesterday.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

        Map<String, Long> typeCountMap = list.stream()
                .filter(x -> !ObjectUtil.isNull(x.getMemberType())
                        && x.getCreateTime().getTime() >= startTimestamp
                        && x.getCreateTime().getTime() <= endTimestamp)
                .collect(Collectors.groupingBy(EPersonnel::getMemberType, Collectors.counting()));
        //返回给前端的数据结构
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(typeCountMap)) {
            for (String key : typeCountMap.keySet()) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", key);
                map.put("value", typeCountMap.get(key));
                mapList.add(map);
            }
        }
        return AjaxResult.success(mapList);
    }
}
